<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>微信jssdk的使用 | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  
  <meta name="description" content="概述和官方文档　　使用微信JSSDK可以在网页中调用手机的拍照语音、定位等手机系统功能，还可以定制微信分享、扫一扫、卡券、支付等微信功能。　　官方文档：http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html 　　 调用步骤1.绑定域名　　首先进入微信公众号后台，进入公众号设置=&amp;gt;功能设置=&amp;gt;JS接口安全域">
<meta property="og:type" content="article">
<meta property="og:title" content="微信JSSDK的使用">
<meta property="og:url" content="http://www.hais2.com/2017/03/15/微信JSSDK的使用/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="概述和官方文档　　使用微信JSSDK可以在网页中调用手机的拍照语音、定位等手机系统功能，还可以定制微信分享、扫一扫、卡券、支付等微信功能。　　官方文档：http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html 　　 调用步骤1.绑定域名　　首先进入微信公众号后台，进入公众号设置=&amp;gt;功能设置=&amp;gt;JS接口安全域">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2017-08-02T15:22:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="微信JSSDK的使用">
<meta name="twitter:description" content="概述和官方文档　　使用微信JSSDK可以在网页中调用手机的拍照语音、定位等手机系统功能，还可以定制微信分享、扫一扫、卡券、支付等微信功能。　　官方文档：http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html 　　 调用步骤1.绑定域名　　首先进入微信公众号后台，进入公众号设置=&amp;gt;功能设置=&amp;gt;JS接口安全域">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-微信JSSDK的使用" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      微信JSSDK的使用
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2017/03/15/微信JSSDK的使用/" class="article-date">
	  <time datetime="2017-03-15T12:33:03.000Z" itemprop="datePublished">March 15, 2017</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="概述和官方文档"><a href="#概述和官方文档" class="headerlink" title="概述和官方文档"></a>概述和官方文档</h2><p>　　使用微信JSSDK可以在网页中调用手机的拍照语音、定位等手机系统功能，还可以定制微信分享、扫一扫、卡券、支付等微信功能。<br>　　官方文档：<a href="http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html" target="_blank" rel="noopener">http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html</a>
　　</p>
<h2 id="调用步骤"><a href="#调用步骤" class="headerlink" title="调用步骤"></a>调用步骤</h2><h3 id="1-绑定域名"><a href="#1-绑定域名" class="headerlink" title="1.绑定域名"></a>1.绑定域名</h3><p>　　首先进入<a href="http://mp.weixin.qq.com/" target="_blank" rel="noopener">微信公众号后台</a>，进入公众号设置=&gt;功能设置=&gt;JS接口安全域名设置：在这里可以设置三个调用的JSSDK的域名，然后将提示信息注意事项3中的<a href="https://mp.weixin.qq.com/cgi-bin/mpverifytxt?lang=zh_CN&amp;token=1492992380" target="_blank" rel="noopener">文件下载</a>下来，放到网站的根目录下，点击保存，微信会自动测试是否设置成功。</p>
<h3 id="2-页面引入JS文件"><a href="#2-页面引入JS文件" class="headerlink" title="2.页面引入JS文件"></a>2.页面引入JS文件</h3><p>　　在需要使用到JSSDK的页面引入微信提供的js文件，可以支持https协议，下载：<a id="more"></a><br> 　　<a href="http://res.wx.qq.com/open/js/jweixin-1.2.0.js" target="_blank" rel="noopener">http://res.wx.qq.com/open/js/jweixin-1.2.0.js</a><br> 　　<a href="https://res.wx.qq.com/open/js/jweixin-1.2.0.js" target="_blank" rel="noopener">https://res.wx.qq.com/open/js/jweixin-1.2.0.js</a><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">@section scripts&#123;</span><br><span class="line">    &lt;script src=<span class="string">"http://res.wx.qq.com/open/js/jweixin-1.2.0.js"</span>&gt;<span class="xml"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="3-请求配置参数，注入微信权限验证配置"><a href="#3-请求配置参数，注入微信权限验证配置" class="headerlink" title="3.请求配置参数，注入微信权限验证配置"></a>3.请求配置参数，注入微信权限验证配置</h3><p>　　在所有使用JSSDK的页面上，必须在打开页面之前注入配置信息，否则无法调用JSSDK，所以，在页面准备完成之前，我们需要操作js代码，对权限验证的参数进行获取。在这里我们需要注意两点：<br>　　1.假设我们在后台可以直接拿到当前公众号的appId。如果后天拿不到就要想办法在页面上拿到appid，然后发送到后台，否则无法进行权限验证。<br>　　2.请求配置需要的<code>jsapi_ticket</code>参数需要缓存下来，避免频繁请求，过期时间在请求成功返回的参数里面带有。<br><em>asp.net mvc:</em><br><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 保存需要返回的数据</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">JsSdkConfigData</span>&#123;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 公众号的app id</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> AppId  &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 生成签名的时间戳</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> Timestamp &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 生成签名的随机字符串</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> NonceStr &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 生成的签名</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> Signature &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 获取页面的配置</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">public</span> JsSdkConfigData <span class="title">GetJsSdkConfig</span>(<span class="params"><span class="keyword">string</span> appid</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> ticket = GetJsApiTicketByAppId(appid);  <span class="comment">//根据appid获取的jsapi_ticket</span></span><br><span class="line">    <span class="keyword">var</span> timestamp = GetTimeStamp(); <span class="comment">//生成的时间戳</span></span><br><span class="line">    <span class="keyword">var</span> nonceStr = Guid.NewGuid().ToString(<span class="string">"N"</span>); <span class="comment">//随机32位字符串</span></span><br><span class="line">    <span class="keyword">var</span> url = currentUrl;  <span class="comment">//注意currentUrl是一个假参数，代表当前请求的页面的url，不能包含#之后的内容</span></span><br><span class="line">    <span class="comment">//拼接字符串</span></span><br><span class="line">    <span class="keyword">var</span> urlStr = <span class="string">$"jsapi_ticket=<span class="subst">&#123;ticket&#125;</span>&amp;noncestr=<span class="subst">&#123;nonceStr&#125;</span>&amp;timestamp=<span class="subst">&#123;timestamp&#125;</span>&amp;url=<span class="subst">&#123;url&#125;</span>"</span>;</span><br><span class="line">    <span class="comment">//对拼接的字符串进行SHA1计算，并拿到小写后的值，就是最终的凭据</span></span><br><span class="line">    <span class="keyword">var</span> signature = SHA1UtilHelper.GetSha1(urlStr).ToLower();</span><br><span class="line">    <span class="comment">//将当前计算凭据的数据都保存起来，返回到后台，因为在验证的时候，凭据和生成它的参数必须保持一致</span></span><br><span class="line">    <span class="keyword">var</span> config=<span class="keyword">new</span> JsSdkConfigData()</span><br><span class="line">    &#123;</span><br><span class="line">        AppId= appid,</span><br><span class="line">        Timestamp= timestamp,</span><br><span class="line">        NonceStr= nonceStr,</span><br><span class="line">        Signature= signature</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">return</span> config;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 根据appid获取jsapi_ticket</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">string</span> <span class="title">GetJsApiTicketByAppId</span>(<span class="params"><span class="keyword">string</span> appId</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//优先读取redis中的缓存数据</span></span><br><span class="line">    <span class="keyword">var</span> cache = CacheManager.GetCache(JsApiTicket)</span><br><span class="line">    <span class="keyword">var</span> cacheToke = cache.GetOrDefault(appId);</span><br><span class="line">    <span class="keyword">if</span> (cacheToke != <span class="literal">null</span> &amp;&amp; !cacheToke.ToString().IsNullOrEmpty()) <span class="keyword">return</span> cacheToke.ToString();</span><br><span class="line">    <span class="comment">//不存在jsapi_ticket,重新获取</span></span><br><span class="line">    <span class="keyword">var</span> publicEntity = WeiXin.GetAll().Single(d =&gt; d.AppId == appId);   <span class="comment">//从数据库读取该公众号的信息</span></span><br><span class="line">    <span class="comment">//借用Senparc.Weixin SDK请求jsapi_ticket(注意token同样需要缓存)</span></span><br><span class="line">    <span class="keyword">var</span> result = CommonApi.GetTicketByAccessToken(GetTokenByAppId(appId));  </span><br><span class="line">    cache.Set(publicEntity.AppId, result.ticket, TimeSpan.FromSeconds(result.expires_in));</span><br><span class="line">    <span class="keyword">return</span> result.ticket;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 根据appid获取token</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="appId"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">string</span> <span class="title">GetTokenByAppId</span>(<span class="params"><span class="keyword">string</span> appId</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">//优先读取redis中缓存的token</span></span><br><span class="line">    <span class="keyword">var</span> cache = CacheManager.GetCache(iCTRWeiXinCacheNames.WeiXinToken)</span><br><span class="line">    <span class="keyword">var</span> cacheToke = cache.GetOrDefault(appId);</span><br><span class="line">    <span class="keyword">if</span> (cacheToke != <span class="literal">null</span> &amp;&amp; !cacheToke.ToString().IsNullOrEmpty()) <span class="keyword">return</span> cacheToke.ToString();</span><br><span class="line">    <span class="comment">//不存在Token,重新获取</span></span><br><span class="line">    <span class="keyword">var</span> Entity = WeiXin.GetAll().Single(d =&gt; d.AppId == appId);  <span class="comment">//从数据库读取该公众号的信息</span></span><br><span class="line">    <span class="comment">//借用Senparc.Weixin SDK请求token</span></span><br><span class="line">    <span class="keyword">var</span> token = AccessTokenContainer.TryGetAccessToken(appId, Entity.AppSecret, <span class="literal">true</span>);  </span><br><span class="line">    cache.Set(Entity.AppId, token, TimeSpan.FromMinutes(<span class="number">110</span>));</span><br><span class="line">    <span class="keyword">return</span> token;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">SHA1UtilHelper</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 签名算法</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="str"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">string</span> <span class="title">GetSha1</span>(<span class="params"><span class="keyword">string</span> str</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//建立SHA1对象</span></span><br><span class="line">        SHA1 sha = <span class="keyword">new</span> SHA1CryptoServiceProvider();</span><br><span class="line">        <span class="comment">//将mystr转换成byte[] </span></span><br><span class="line">        ASCIIEncoding enc = <span class="keyword">new</span> ASCIIEncoding();</span><br><span class="line">        <span class="keyword">byte</span>[] dataToHash = enc.GetBytes(str);</span><br><span class="line">        <span class="comment">//Hash运算</span></span><br><span class="line">        <span class="keyword">byte</span>[] dataHashed = sha.ComputeHash(dataToHash);</span><br><span class="line">        <span class="comment">//将运算结果转换成string</span></span><br><span class="line">        <span class="keyword">string</span> hash = BitConverter.ToString(dataHashed).Replace(<span class="string">"-"</span>, <span class="string">""</span>);</span><br><span class="line">        <span class="keyword">return</span> hash;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><em>JavaScript:</em><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        $.get(&#123;</span><br><span class="line">            url:<span class="string">"/WeiXin/GetJssdkConfig"</span>,</span><br><span class="line">            success:<span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>&#123;</span><br><span class="line">                <span class="built_in">window</span>.wx.config(&#123;</span><br><span class="line">                        debug: <span class="literal">true</span>, <span class="comment">// 开启    调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。</span></span><br><span class="line">                        appId: result.appId, <span class="comment">// 必填，公众号的唯一标识</span></span><br><span class="line">                        timestamp: result.timestamp, <span class="comment">// 必填，生成签名的时间戳</span></span><br><span class="line">                        nonceStr: result.nonceStr, <span class="comment">// 必填，生成签名的随机串</span></span><br><span class="line">                        signature: result.signature,<span class="comment">// 必填，签名，</span></span><br><span class="line">                        jsApiList: [</span><br><span class="line">                            <span class="string">'onMenuShareTimeline'</span>,</span><br><span class="line">                            <span class="string">'onMenuShareAppMessage'</span>,</span><br><span class="line">                            <span class="string">'onMenuShareQQ'</span>,</span><br><span class="line">                            <span class="string">'onMenuShareWeibo'</span>] <span class="comment">// 必填，需要使用的JS接口列表，</span></span><br><span class="line">                    &#125;);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">//通过ready接口处理成功验证</span></span><br><span class="line">    wx.ready(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">                wx.onMenuShareAppMessage(&#123;</span><br><span class="line">                    title: <span class="string">'用户点击发送给朋友'</span>,</span><br><span class="line">                    desc: <span class="string">'用户点击发送给朋友'</span>,</span><br><span class="line">                    link: <span class="built_in">window</span>.location.href,</span><br><span class="line">                    imgUrl: <span class="string">'http://ww4.sinaimg.cn/large/c55a7aeejw1fa2eocj9c4j20dc07ijrg.jpg'</span>,</span><br><span class="line">                    trigger: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'用户点击发送给朋友'</span> + <span class="built_in">window</span>.location.href);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    success: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'已分享'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    cancel: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'已取消'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    fail: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="built_in">JSON</span>.stringify(res));</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">                wx.onMenuShareTimeline(&#123;</span><br><span class="line">                    title: <span class="string">'用户点击分享到朋友圈'</span>,</span><br><span class="line">                    link: <span class="built_in">window</span>.location.href,</span><br><span class="line">                    imgUrl: <span class="string">'http://ww4.sinaimg.cn/large/c55a7aeejw1fa2eocj9c4j20dc07ijrg.jpg'</span>,</span><br><span class="line">                    trigger: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'用户点击分享到朋友圈'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    success: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'已分享'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    cancel: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'已取消'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    fail: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="built_in">JSON</span>.stringify(res));</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">                wx.onMenuShareQQ(&#123;</span><br><span class="line">                    title: <span class="string">'用户点击分享到QQ'</span>,</span><br><span class="line">                    desc: <span class="string">'用户点击分享到QQ'</span>,</span><br><span class="line">                    link: <span class="built_in">window</span>.location.href,</span><br><span class="line">                    imgUrl: <span class="string">'http://ww4.sinaimg.cn/large/c55a7aeejw1fa2eocj9c4j20dc07ijrg.jpg'</span>,</span><br><span class="line">                    trigger: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'用户点击分享到QQ'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    complete: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="built_in">JSON</span>.stringify(res));</span><br><span class="line">                    &#125;,</span><br><span class="line">                    success: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'已分享'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    cancel: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'已取消'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    fail: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="built_in">JSON</span>.stringify(res));</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">                wx.onMenuShareWeibo(&#123;</span><br><span class="line">                    title: <span class="string">'用户点击分享到微博'</span>,</span><br><span class="line">                    desc: <span class="string">'用户点击分享到微博'</span>,</span><br><span class="line">                    link: <span class="built_in">window</span>.location.href,</span><br><span class="line">                    imgUrl: <span class="string">'http://ww4.sinaimg.cn/large/c55a7aeejw1fa2eocj9c4j20dc07ijrg.jpg'</span>,</span><br><span class="line">                    trigger: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'用户点击分享到微博'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    complete: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="built_in">JSON</span>.stringify(res));</span><br><span class="line">                    &#125;,</span><br><span class="line">                    success: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'已分享'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    cancel: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="string">'已取消'</span>);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    fail: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">                        alert(<span class="built_in">JSON</span>.stringify(res));</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">            &#125;);</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure></p>
<h2 id="结束"><a href="#结束" class="headerlink" title="结束"></a>结束</h2><p>　　其他使用方法，参看官方文档。</p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
      
      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2017/03/15/Raspberry-pie3-deployed-on-the-qiandao.today-check-in-procedures/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          在树莓派3上部署qiandao.today签到程序
        
      </div>
    </a>
  
  
    <a href="/2017/02/26/use-ckeditor-error/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">使用CKEditor时碰到的一个坑</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#概述和官方文档"><span class="nav-number">1.</span> <span class="nav-text">概述和官方文档</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#调用步骤"><span class="nav-number">2.</span> <span class="nav-text">调用步骤</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-绑定域名"><span class="nav-number">2.1.</span> <span class="nav-text">1.绑定域名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-页面引入JS文件"><span class="nav-number">2.2.</span> <span class="nav-text">2.页面引入JS文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-请求配置参数，注入微信权限验证配置"><span class="nav-number">2.3.</span> <span class="nav-text">3.请求配置参数，注入微信权限验证配置</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结束"><span class="nav-number">3.</span> <span class="nav-text">结束</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
